package com.test.mchelepesa.activity_fragment_rice.info_rice;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.test.mchelepesa.R;
import com.test.mchelepesa.activity_fragment_rice.base_rice.BaseInfoActivity_rice;
import com.test.mchelepesa.bean_rice.config_info_rice.AppShowInfo_rice;
import com.test.mchelepesa.bean_rice.user_info_rice.BankAccountList_rice;
import com.test.mchelepesa.dialog_rice.BindAccountDialog_rice;
import com.test.mchelepesa.dialog_rice.BindAccountTipsDialog_rice;
import com.test.mchelepesa.dialog_rice.BindChannelDialog_rice;
import com.test.mchelepesa.dialog_rice.DeleteAccountDialog_rice;
import com.test.mchelepesa.dialog_rice.SubmitAccountDialog_rice;
import com.test.mchelepesa.util_rice.StringUtil_rice;
import com.test.mchelepesa.util_rice.http_rice.HttpManager_rice;
import com.test.mchelepesa.view_rice.widget_rice.TitleView_rice;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

public class BindAccountActivity_rice extends BaseInfoActivity_rice {

    private LinearLayout groupLayout;

    private boolean addBindFlag = false;
    private String bindAccountOldChannel = "";
    private String bindAccountMobile = "";
    private String bindAccountNewChannel = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind_account_rice);

        TitleView_rice titleView = findViewById(R.id.bind_account_title);
        titleView.getBackIb().setOnClickListener(v -> onBackPressed());

        groupLayout = findViewById(R.id.bind_account_group);

        initLicense(
                findViewById(R.id.bind_account_view_license),
                findViewById(R.id.bind_account_license_logo),
                findViewById(R.id.bind_account_license_company_name)
        );

        TextView accountTipsTv = findViewById(R.id.bind_account_tips);
        String tips = AppShowInfo_rice.getInstance().getBindAccountTips();
        if (!TextUtils.isEmpty(tips)) accountTipsTv.setText(tips);

        findViewById(R.id.bind_account_add).setOnClickListener(v -> {
            HttpManager_rice.getInstance().saveUserBuriedPoint("ACCOUNT_INFO_ADD_CLICK");
            new BindAccountDialog_rice(this,(mobile,channel) -> {
                this.bindAccountMobile = mobile;
                bindAccountOldChannel = channel;
            }).show();
        });

        findViewById(R.id.bind_account_next).setOnClickListener(v -> {

            HttpManager_rice.getInstance().saveUserBuriedPointAndUpload("ACCOUNT_INFO_NEXT_CLICK");

            int accountNum = BankAccountList_rice.getInstance().getBankAccountList().size();

            if (accountNum==0){
                showToast_rice("Veuillez ajouter au moins un compte.");
                return;
            }

            if (accountNum<2){
                new BindAccountTipsDialog_rice(this,() -> new SubmitAccountDialog_rice(this).show()).show();
                return;
            }

            new SubmitAccountDialog_rice(this).show();

        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        HttpManager_rice.getInstance().fetchBankAccount();
    }

    public void onFetchAccountListResponse(){
        refreshAccountList();
    }

    public void onAddAccountResponse(boolean addBindFlag,String channel){
        this.addBindFlag = addBindFlag;
        this.bindAccountNewChannel = channel;
        HttpManager_rice.getInstance().fetchBankAccount();
    }

    public void onDeleteAccountResponse(){
        HttpManager_rice.getInstance().fetchBankAccount();
    }

    private void refreshAccountList(){

        groupLayout.removeAllViews();

        ArrayList<BankAccountList_rice.Account> accountList = BankAccountList_rice.getInstance().getBankAccountList();

        int count = 0;

        for (int i = accountList.size()-1;i>=0;i--){

            BankAccountList_rice.Account account = accountList.get(i);

            count++;

            View item = View.inflate(this,R.layout.item_account,null);

            TextView titleTv = item.findViewById(R.id.item_account_title);
            ImageView logoIv = item.findViewById(R.id.item_account_iv_logo);
            TextView channelTv = item.findViewById(R.id.item_account_tv_channel);
            TextView mobileTv = item.findViewById(R.id.item_account_tv_mobile);
            View deleteView = item.findViewById(R.id.item_account_delete);

            if (count>1) titleTv.setText(getString(R.string.bind_account_mobile_wallet,count));

            String logoUrl = account.getWalletLogo();
            if (!TextUtils.isEmpty(logoUrl)){
                new Thread(() -> {
                    try {
                        URL url = new URL(logoUrl);
                        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                        connection.setConnectTimeout(4995);
                        connection.setRequestMethod("GET");
                        if (connection.getResponseCode()==200){
                            InputStream inputStream = connection.getInputStream();
                            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                            runOnUiThread(() -> logoIv.setImageBitmap(bitmap));
                        }
                    }catch (Exception e){
                        StringUtil_rice.printException(e);
                    }
                }).start();
            }

            channelTv.setText(account.getWalletDesc());
            mobileTv.setText(account.getWalletMobile());

            deleteView.setOnClickListener(v -> {
                HttpManager_rice.getInstance().saveUserBuriedPoint("ACCOUNT_INFO_DELETE_ACCOUNT_CLICK",account.getWalletDesc()+" "+account.getWalletMobile());
                new DeleteAccountDialog_rice(this,account).show();
            });

            groupLayout.addView(item);

        }

        while (count<2){

            count++;

            View item = View.inflate(this,R.layout.item_bind_account_edit,null);

            TextView titleTv = item.findViewById(R.id.edit_account_title);

            if (count>1) titleTv.setText(getString(R.string.bind_account_mobile_wallet,count));

            int finalCount = count;
            item.findViewById(R.id.edit_account_view_click).setOnClickListener(v -> {
                HttpManager_rice.getInstance().saveUserBuriedPoint("ACCOUNT_INFO_EDIT_CLICK",String.valueOf(finalCount));
                new BindAccountDialog_rice(this,(mobile,channel) -> {
                    this.bindAccountMobile = mobile;
                    bindAccountOldChannel = channel;
                }).show();
            });

            groupLayout.addView(item);

        }

        if (addBindFlag && !TextUtils.isEmpty(bindAccountOldChannel)  && !TextUtils.isEmpty(bindAccountNewChannel) && !TextUtils.isEmpty(bindAccountMobile)){

            AppShowInfo_rice.WalletChannel channel = AppShowInfo_rice.getInstance().findChannelByValue(bindAccountNewChannel);

            if (channel!=null) new BindChannelDialog_rice(this,bindAccountMobile,bindAccountOldChannel,channel).show();

            addBindFlag = false;
            bindAccountOldChannel = "";
            bindAccountNewChannel = "";
            bindAccountMobile = "";
        }

    }

}